﻿Imports DTO
Imports System.Data.Linq

Public Class DocGiaDao
    Public Shared Function LayBang() As Table(Of DocGia)
        Dim db As New ThuVienDataContext()
        Return db.DocGias
    End Function

    Public Shared Function LayBang_Tracuu() As Object
        Dim db As New ThuVienDataContext()
        Dim kq = From t In db.TheThuViens _
                 From d In db.DocGias _
                 Where t.MDG = d.MDG And t.Da_huy = False _
                 Select t.MT, d.Ho_ten, d.Cmnd, d.Dia_chi, d.Dien_thoai, d.Gioi_tinh
        Return kq
    End Function

    Public Shared Function LayBang_Tracuu_Theomathe(ByVal m As Integer) As Object
        Dim db As New ThuVienDataContext()
        Dim kq = From t In db.TheThuViens _
                 From d In db.DocGias _
                 Where t.MT = m And t.MDG = d.MDG And t.Da_huy = False _
                 Select t.MT, d.Ho_ten, d.Cmnd, d.Dia_chi, d.Dien_thoai, d.Gioi_tinh
        Return kq
    End Function

    Public Shared Function LayBang_Tracuu_Theohoten(ByVal ht As String) As Object
        Dim db As New ThuVienDataContext()
        Dim kq = From t In db.TheThuViens _
                 From d In db.DocGias _
                 Where d.Ho_ten.Contains(ht) And t.MDG = d.MDG And t.Da_huy = False _
                 Select t.MT, d.Ho_ten, d.Cmnd, d.Dia_chi, d.Dien_thoai, d.Gioi_tinh
        Return kq
    End Function

    Public Shared Function LayBang_Tracuu_Theocmnd(ByVal cm As String) As Object
        Dim db As New ThuVienDataContext()
        Dim kq = From t In db.TheThuViens _
                 From d In db.DocGias _
                 Where d.Cmnd.Contains(cm) And t.MDG = d.MDG And t.Da_huy = False _
                 Select t.MT, d.Ho_ten, d.Cmnd, d.Dia_chi, d.Dien_thoai, d.Gioi_tinh
        Return kq
    End Function

    Public Shared Function LayDanhSach() As IEnumerable(Of DocGia)
        Dim lst As IEnumerable(Of DocGia)
        Dim db As New ThuVienDataContext()
        lst = From dg In db.DocGias Select dg
        Return lst
    End Function

    Public Shared Function LayDocgia(ByVal m As Integer) As DocGia
        Dim dg As DocGia
        Dim db As New ThuVienDataContext()
        dg = (From d In db.DocGias Where d.MDG = m Select d).Single
        Return dg
    End Function

    Public Shared Sub Them(ByVal t As DocGia)
        Dim db As New ThuVienDataContext()
        db.DocGias.InsertOnSubmit(t)
        db.SubmitChanges()
    End Sub

    Public Shared Sub ThemDocGia(ByVal d As DocGia, ByVal t As TheThuVien)
        Dim db As New ThuVienDataContext()
        db.DocGias.InsertOnSubmit(d)
        db.SubmitChanges()
        t.MDG = d.MDG
        db.TheThuViens.InsertOnSubmit(t)
        db.SubmitChanges()
    End Sub

    Public Shared Sub CapNhat(ByVal docgia As DocGia)
        Dim db As New ThuVienDataContext()
        Dim dg = (From d In db.DocGias Where d.MDG = docgia.MDG Select d).Single

        dg.Ho_ten = docgia.Ho_ten
        dg.Cmnd = docgia.Cmnd
        dg.Dia_chi = docgia.Dia_chi
        dg.Dien_thoai = docgia.Dien_thoai
        dg.Gioi_tinh = docgia.Gioi_tinh

        db.SubmitChanges()
    End Sub

    Public Shared Sub Xoa(ByVal m As Integer)
        Dim db As New ThuVienDataContext
        Dim s = (From t In db.Saches Where t.MS = m Select t).Single
        db.Saches.DeleteOnSubmit(s)
        db.SubmitChanges()
    End Sub
End Class
